﻿' BasecampVB - http://www.tidbitsandtangents.com
' Copyright (c) 2009
' by Rusty S. Swayne
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports TnT.BasecampVB.DAL

Namespace BLL

    Public Class ToDoItem
        Inherits BasecampBase

        ''' <summary>
        ''' Constructor
        ''' </summary>
        Public Sub New( _
            ByVal todo_item_id As Integer, _
            ByVal todo_list_id As Integer, _
            ByVal creator_id As Integer, _
            ByVal content As String, _
            ByVal comments_count As Integer, _
            ByVal position As Integer, _
            ByVal responsible_party_id As Integer, _
            ByVal responsible_party_type As String, _
            ByVal due_at As DateTime, _
            ByVal completed As Boolean, _
            ByVal created_at As DateTime, _
            ByVal created_on As DateTime)

            Me.todo_item_id = todo_item_id
            Me.todo_list_id = todo_list_id
            Me.creator_id = creator_id
            Me.content = content
            Me.comments_count = comments_count
            Me.position = position
            Me.responsible_party_id = responsible_party_id
            Me.responsible_party_type = responsible_party_type
            Me.due_at = due_at
            Me.completed = completed
            Me.created_at = created_at
            Me.created_on = created_on

        End Sub

#Region "ToDoItem"

        ''' <summary>
        ''' Returns ToDoItem from Basecamp API
        ''' </summary>
        Public Shared Function GetToDoItem(ByVal todo_item_id As Integer) As ToDoItem
            Return GetToDoItemFromToDoItemDetails(BasecampProvider.API.GetToDoItem(todo_item_id))
        End Function

        ''' <summary>
        ''' Returns a List(Of ToDoItem) from Basecamp API
        ''' </summary>
        Public Shared Function GetToDoItems(ByVal todo_list_id As Integer) As List(Of ToDoItem)
            Return GetToDoItemListFromToDoItemDetailsCollection(BasecampProvider.API.GetToDoItems(todo_list_id))
        End Function

        ''' <summary>
        ''' Sets a ToDoItem to complete in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function CompleteToDoItem(ByVal todo_item_id As Integer) As Boolean
            Return BasecampProvider.API.CompleteToDoItem(todo_item_id)
        End Function

        ''' <summary>
        ''' Sets a ToDoItem to complete in Basecamp via Basecamp API
        ''' </summary>
        Public Function Complete() As Boolean
            Return CompleteToDoItem(Me.todo_item_id)
        End Function

        ''' <summary>
        ''' Sets a ToDoItem to incomplete in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function UncompleteToDoItem(ByVal todo_item_id As Integer) As Boolean
            Return BasecampProvider.API.UncompleteToDoItem(todo_item_id)
        End Function

        ''' <summary>
        ''' Sets a ToDoItem to incomplete in Basecamp via Basecamp API
        ''' </summary>
        Public Function Uncomplete() As Boolean
            Return UncompleteToDoItem(Me.todo_item_id)
        End Function

        ''' <summary>
        ''' Creates a ToDoItem in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function CreateToDoItem(ByVal todo_list_id As Integer, ByVal content As String, ByVal due_at As DateTime, ByVal notify As Boolean) As Integer
            Return BasecampProvider.API.CreateToDoItem(todo_list_id, content, due_at, notify, "")
        End Function

        ''' <summary>
        ''' Creates a ToDoItem in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function CreateToDoItem(ByVal todo_list_id As Integer, ByVal content As String, ByVal due_at As DateTime, ByVal notify As Boolean, ByVal responsible_party_id As Integer, ByVal responsible_party_type As ResponsiblePartyType) As Integer            
            Return BasecampProvider.API.CreateToDoItem(todo_list_id, content, due_at, notify, GetResponsiblePartyString(responsible_party_id, responsible_party_type))
        End Function

        ''' <summary>
        ''' Updates a ToDoItem in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function UpdateToDoItem(ByVal todo_item_id As Integer, ByVal content As String, ByVal due_at As DateTime, ByVal notify As Boolean) As Boolean
            Return BasecampProvider.API.UpdateToDoItem(todo_item_id, content, due_at, notify, "")
        End Function

        ''' <summary>
        ''' Updates a ToDoItem in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function UpdateToDoItem(ByVal todo_item_id As Integer, ByVal content As String, ByVal due_at As DateTime, ByVal notify As Boolean, ByVal responsible_party_id As Integer, ByVal responsible_party_type As ResponsiblePartyType) As Boolean
            Return BasecampProvider.API.UpdateToDoItem(todo_item_id, content, due_at, notify, GetResponsiblePartyString(responsible_party_id, responsible_party_type))
        End Function

        ''' <summary>
        ''' Updates a ToDoItem in Basecamp via Basecamp API
        ''' </summary>
        Public Function Update()
            Dim rp As ResponsiblePartyType = ResponsiblePartyType.person
            If Me.responsible_party_type.ToLower = "company" Then rp = ResponsiblePartyType.company
            Return UpdateToDoItem(Me.todo_item_id, Me.content, Me.due_at, False, Me.responsible_party_id, rp)
        End Function

        ''' <summary>
        ''' Deletes a ToDoItem in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function DeleteToDoItem(ByVal todo_item_id As Integer) As Boolean
            Return BasecampProvider.API.DeleteToDoItem(todo_item_id)
        End Function

        ''' <summary>
        ''' Deletes a ToDoItem in Basecamp via Basecamp API
        ''' </summary>
        Public Function Delete() As Boolean
            Dim ret As Boolean = DeleteToDoItem(Me.todo_item_id)
            Me.todo_item_id = 0
            Return ret
        End Function

        ''' <summary>
        ''' Reorders ToDoItems in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function ReorderToDoItems(ByVal todo_list_id As Integer, ByVal todoitems As List(Of ToDoItem)) As Boolean
            Dim ret As Boolean = False
            Dim ids As New List(Of Integer)
            If todoitems IsNot Nothing Then
                Dim tdi As ToDoItem = Nothing
                For Each tdi In todoitems
                    ids.Add(tdi.todo_item_id)
                Next
                ret = BasecampProvider.API.ReorderToDoItems(todo_list_id, ids)
            End If
            Return ret
        End Function

#End Region


#Region "Comments"

        ''' <summary>
        ''' Adds a comment to the currently instantiated ToDoItem in Basecamp via Basecamp API
        ''' </summary>
        Public Function AddComment(ByVal body As String) As Integer
            Return BasecampProvider.API.CreateComment(CommentResource.todo_items, Me.todo_item_id, body)
        End Function

#End Region



#Region "Private Utility Methods"

        ''' <summary>
        ''' Helper Function to create a List(Of ToDoItem) from List(Of ToDoItemDetails) 
        ''' </summary>
        Private Shared Function GetToDoItemListFromToDoItemDetailsCollection(ByVal recordset As List(Of ToDoItemDetails)) As List(Of ToDoItem)
            Dim items As New List(Of ToDoItem)
            Dim item As ToDoItemDetails = Nothing
            For Each item In recordset
                items.Add(GetToDoItemFromToDoItemDetails(item))
            Next
            Return items
        End Function

        ''' <summary>
        ''' Helper Function to create a ToDoItem from ToDoItemDetails
        ''' </summary>
        Private Shared Function GetToDoItemFromToDoItemDetails(ByVal record As ToDoItemDetails) As ToDoItem
            Return New ToDoItem(record.todo_item_id, record.todo_list_id, record.creator_id, record.content, record.comments_count, record.position, record.responsible_party_id, _
                            record.responsible_party_type, record.due_at, record.completed, record.created_at, record.created_on)
        End Function

#End Region


#Region "Custom Properties"

        ''' <summary>
        ''' Returns the ToDoList associated with currently instantiated ToDoItem
        ''' </summary>
        Public ReadOnly Property todo_list() As ToDoList
            Get
                Return ToDoList.GetToDoList(Me.todo_list_id)
            End Get
        End Property

        ''' <summary>
        ''' Returns List(Of comment) asscociated with currently instantiated ToDoItem
        ''' </summary>
        Public ReadOnly Property comments() As List(Of Comment)
            Get
                Return Comment.GetComments(CommentResource.todo_items, Me.todo_item_id)
            End Get
        End Property

#End Region

#Region "Properties"

        Private _todo_item_id As Integer
        Public Property todo_item_id() As Integer
            Get
                Return _todo_item_id
            End Get
            Set(ByVal value As Integer)
                _todo_item_id = value
            End Set
        End Property

        Private _todo_list_id As Integer
        Public Property todo_list_id() As Integer
            Get
                Return _todo_list_id
            End Get
            Set(ByVal value As Integer)
                _todo_list_id = value
            End Set
        End Property

        Private _creator_id As Integer
        Public Property creator_id() As Integer
            Get
                Return _creator_id
            End Get
            Set(ByVal value As Integer)
                _creator_id = value
            End Set
        End Property


        Private _content As String
        Public Property content() As String
            Get
                Return _content
            End Get
            Set(ByVal value As String)
                _content = value
            End Set
        End Property

        Private _comments_count As Integer
        Public Property comments_count() As Integer
            Get
                Return _comments_count
            End Get
            Set(ByVal value As Integer)
                _comments_count = value
            End Set
        End Property

        Private _position As Integer
        Public Property position() As Integer
            Get
                Return _position
            End Get
            Set(ByVal value As Integer)
                _position = value
            End Set
        End Property

        Private _responsible_party_id As Integer
        Public Property responsible_party_id() As Integer
            Get
                Return _responsible_party_id
            End Get
            Set(ByVal value As Integer)
                _responsible_party_id = value
            End Set
        End Property

        Private _responsible_party_type As String
        Public Property responsible_party_type() As String
            Get
                Return _responsible_party_type
            End Get
            Set(ByVal value As String)
                _responsible_party_type = value
            End Set
        End Property

        Private _due_at As DateTime
        Public Property due_at() As DateTime
            Get
                Return _due_at
            End Get
            Set(ByVal value As DateTime)
                _due_at = value
            End Set
        End Property

        Private _completed As Boolean
        Public Property completed() As Boolean
            Get
                Return _completed
            End Get
            Set(ByVal value As Boolean)
                _completed = value
            End Set
        End Property

        Private _created_on As DateTime
        Public Property created_on() As DateTime
            Get
                Return _created_on
            End Get
            Set(ByVal value As DateTime)
                _created_on = value
            End Set
        End Property


        Private _created_at As DateTime
        Public Property created_at() As DateTime
            Get
                Return _created_at
            End Get
            Set(ByVal value As DateTime)
                _created_at = value
            End Set
        End Property


#End Region


    End Class

End Namespace
